package com.gopay.common.manager.ptbank.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.gopay.common.constants.ptbank.HfBankConstants.BankServiceState;
import com.gopay.common.dao.ptbank.GsrtBankCustSuperviseInfoDao;
import com.gopay.common.domain.ptbank.GsrtBankCustSuperviseInfo;
import com.gopay.common.domain.ptbank.vo.GsrtBankCustSuperviseInfoVo;
import com.gopay.common.manager.ptbank.GsrtBankCustSuperviseInfoManager;
import com.gopay.common.order.dao.CommonQueryDAO;

/**
 * G商融通商户和托管银行关系表 ManagerImpl GsrtBankCustSuperviseInfoManagerImpl.java
 * 2016年4月27日
 * 
 * @author jlj www.gopay.com.cn Inc.All rights reserved.
 */
@Service("gsrtBankCustSuperviseInfoManager")
public class GsrtBankCustSuperviseInfoManagerImpl implements GsrtBankCustSuperviseInfoManager {

	@Resource(name = "commonQueryDAO")
	private CommonQueryDAO commonQueryDAO;

	@Resource(name = "gsrtBankCustSuperviseInfoDao")
	private GsrtBankCustSuperviseInfoDao gsrtBankCustSuperviseInfoDao;

	@Override
    public List<GsrtBankCustSuperviseInfoVo> getlist(String merCustId) {
		StringBuffer bf = new StringBuffer();
		bf.append("  select " + "  bank.bank_supervise_name,bank.bank_supervise_id, "
				+ "  bank.bank_no,bank.bank_code,info.mer_cust_id,info.account_state "
				+ "  from GSRT_BANK_SUPERVISE_NAME  " + "  bank left join gsrt_bank_cust_supervise_info  info ");
		bf.append("  on bank.bank_supervise_id=info.bank_supervise_id  ");
		bf.append(" where ");
		bf.append(" 1=1  ");
		if (StringUtils.isNotBlank(merCustId)) {
			bf.append("  and info.mer_cust_id='" + merCustId + "'   ");
		}
		bf.append("  and  info.bank_state='04' ");
		bf.append("  and info.account_state='02' ");
		bf.append("  and info.cust_bank_state='01'");
		List<Object[]> objectlist = commonQueryDAO.findBySql(bf.toString());

		List<GsrtBankCustSuperviseInfoVo> list = new ArrayList<GsrtBankCustSuperviseInfoVo>();
		GsrtBankCustSuperviseInfoVo vo = null;
		for (Object[] obj : objectlist) {
			vo = new GsrtBankCustSuperviseInfoVo();
			if (obj[0] != null) {
				vo.setBankSuperviseName(obj[0].toString());
			}
			if (obj[1] != null) {
				vo.setBankSuperviseId(obj[1].toString());
			}
			if (obj[2] != null) {
				vo.setBankNo(obj[2].toString());
			}
			if (obj[3] != null) {
				vo.setBankCode(obj[3].toString());
			}
			if (obj[4] != null) {
				vo.setMerCustId(obj[4].toString());
			}
			if (obj[5] != null) {
				vo.setAccountState(obj[5].toString());
			}
		}
		list.add(vo);
		return list;

	}

	@Override
	public List<GsrtBankCustSuperviseInfo> findBankCustInfo(GsrtBankCustSuperviseInfo info) {
		return this.gsrtBankCustSuperviseInfoDao.findByExample(info);
	}

	@Override
    public boolean update(GsrtBankCustSuperviseInfo info) {
		this.gsrtBankCustSuperviseInfoDao.update(info);
		return true;
	}
	@Override
    public boolean updateByHql(GsrtBankCustSuperviseInfo info) {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("bankState", info.getBankState());
		param.put("id", info.getId());
		String hql = "update GsrtBankCustSuperviseInfo g set g.bankState=:bankState where g.id=:id";
		int updateResult = this.gsrtBankCustSuperviseInfoDao.updateByHql(hql, param);
		return updateResult>0;
	}
	@Override
    public List<GsrtBankCustSuperviseInfo> getBankCustSuperviseInfo(String merId){
		if(StringUtils.isEmpty(merId)){
			return null;
		}
    	GsrtBankCustSuperviseInfo info = new GsrtBankCustSuperviseInfo();
    	info.setMerCustId(merId);
    	info.setCustBankState(BankServiceState.OPENED.value);
    	List<GsrtBankCustSuperviseInfo> queryList=this.gsrtBankCustSuperviseInfoDao.findByExample(info);
    	return queryList;
    }
	@Override
	public GsrtBankCustSuperviseInfo findInfoByMerCustId(String merCustId) {
		GsrtBankCustSuperviseInfo gbsi = gsrtBankCustSuperviseInfoDao.getInfoByMerCustId(merCustId);
		return gbsi;
		
	}
	
	@Override
	public void saveInfo(GsrtBankCustSuperviseInfo info) {
		gsrtBankCustSuperviseInfoDao.save(info);
	}	
	
}
